//
//  ChartingViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/29.
//

#import "ChartingViewController.h"
#import "ChartingTopView.h"
#import "ChartingTableViewCell.h"
#import "ChartingModel.h"
#import "ChartingFootView.h"
#import "MyChartingViewController.h"
#import "NoTextView.h"
#import "HotModel.h"
#import "ChartingImageNumberView.h"
#import "DrawingWaitViewController.h"
#import "MyChartingModel.h"
#import "VipView.h"
#import "MeVipViewController.h"
#import "ZSViewController.h"
#import "ChartView.h"
#import "NoIntegral.h"
@interface ChartingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) ChartingTopView * topView;
@property (nonatomic , strong) ChartingFootView * footView;
@end

@implementation ChartingViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{

    UIImageView * bgImage = [[UIImageView alloc]init];
    bgImage.image = SQGetImage(@"背景");
    bgImage.clipsToBounds = YES;
    bgImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bgImage];
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(SCREEN_WIDETH*184/375);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.topView;
    self.tableView.tableFooterView = self.footView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    
    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(scrollViewWillBeginDragging:)];
    WS(weakSelf);
    NSOperationQueue*  mainQueue = [NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {
        [weakSelf.tableView addGestureRecognizer:tap];
    }];
    [nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {
        [weakSelf.tableView removeGestureRecognizer:tap];
    }];
   
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:@"img/imgHome" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc] initWithDictionary:httpModel.data];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray: [ChartingModel SQ_setUpModel:data]];
         self.topView.integral = [[NSString stringWithFormat:@"%@",[data objectForKey:@"integral"]] intValue];
        self.topView.needIntegral =[[NSString stringWithFormat:@"%@",[data objectForKey:@"needIntegral"]] intValue];
         [self.tableView reloadData];
         [self.tableView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.tableView.mj_header endRefreshing];
    }];
}
-(void)SQ_Foother{
    [self.tableView.mj_footer endRefreshing];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.jsonArray .count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.jsonArray.count <=0) {
        return 0;
    }
     return 1;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChartingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChartingTableViewCell"];
    if (!cell) {
        cell = [[ChartingTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"ChartingTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray objectAtIndex:indexPath.section];
    WS(weakSelf);
    [cell setSeletedBlock:^(HotModel * _Nonnull model) {
        [weakSelf SQ_reloadArray:model];
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChartingModel * model = [self.jsonArray objectAtIndex:indexPath.section];
    return  model.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section ==2) {
        return  0;
    }
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ChartingModel * model = [self.jsonArray objectAtIndex:section];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 30)];
    view.backgroundColor = UIColor.clearColor;
    UILabel * la = [[UILabel alloc]initWithFrame:CGRectMake(12, 5, 200, 20)];
    la.text = model.name;
    la.font = FONT_MEDIUM(16);
    la.textColor = SQColor333333;
    [view addSubview:la];
    if (section ==0) {
        UIButton * myCharting = [UIButton buttonWithType:UIButtonTypeCustom];
        myCharting.backgroundColor = SQColor(@"#4AF8CE");
        [myCharting setTitle:SQNSLocalString(@"我的图片薄") forState:normal];
        [myCharting setTitleColor:SQColor333333 forState:normal];
        myCharting.layer.cornerRadius = 13;
        myCharting.layer.masksToBounds = YES;
        myCharting.titleLabel.font = FONT_REGULAR(12);
        [myCharting addTarget:self action:@selector(SQ_MyCharting) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:myCharting];
        [myCharting mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(110);
            make.height.mas_equalTo(26);
            make.centerY.equalTo(view);
            make.right.equalTo(view.mas_right).offset(20);
        }];
    }
    return  view;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //用户手动拖动tableview，隐藏所有面板和键盘
    [self.topView.textField resignFirstResponder];
}
-(void)SQ_reloadArray:(HotModel*)model{
    if (self.jsonArray.count < 3) {
        return;
    }
    ChartingModel * mo = [self.jsonArray objectAtIndex:2];
    if (model.styles.count<=0) {
        return;
    }
    [mo.jsonArray removeAllObjects];
    [mo.jsonArray addObjectsFromArray:[HotModel mj_objectArrayWithKeyValuesArray:model.styles]];
    HotModel * model3 = mo.jsonArray.firstObject;
    model3.isSeleted = YES;
    [self.tableView reloadData];
    
}
 
-(void)SQ_MyCharting{
    MyChartingViewController * vc = [[MyChartingViewController alloc]init];
    if (self.jsonArray.count >= 4) {
        ChartingModel * mo = self.jsonArray.lastObject;
        vc.topArray = [[NSMutableArray alloc]initWithArray:mo.jsonArray];
    }
    [self SQ_pushViewControllerFull:vc];
}
-(void)SQ_addImage{
    if (![self.topView.textField.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString( @"请输入要生成的对象关键词")];
        return;
    }
    if (![UserModel share].isVip) {
        [self pushVIPAlert];
        return;
    }
    NSString * needIntegral = [Manager SQ_stringForKey:@"needIntegral"];
    if ([needIntegral isNotEmpty]) {
        if (self.topView.needIntegral == [needIntegral intValue]) {
            //不弹.
            [self SQ_addImageHttps];
        } else {
            //弹
            [self pushCreateAlert];
        }
    } else {
        //弹
        [self pushCreateAlert];
    }
}
-(void)SQ_addImageHttps{
   
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    ChartingModel * mo1 = [self.jsonArray objectAtIndex:2];
    for (HotModel * mo11 in mo1.jsonArray) {
        if (mo11.isSeleted) {
            [dic setObject:mo11.userid forKey:@"styleId"];
        }
    }
    ChartingModel * mo2 = [self.jsonArray objectAtIndex:3];
    for (HotModel * mo22 in mo2.jsonArray) {
        if (mo22.isSeleted) {
            [dic setObject:mo22.userid forKey:@"sizeId"];
        }
    }
    [dic setObject:self.topView.textField.text forKey:@"content"];
    [HttpTool post:@"img/createImg" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        DrawingWaitViewController * vc = [[DrawingWaitViewController alloc]init];
        vc.model = [MyChartingModel mj_objectWithKeyValues:httpModel.data];
        [self SQ_pushViewControllerFull:vc];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        if (httpModel.code == -4) {
            [self pushChargeAlert];
        } else {
            [self SQ_popcodeView:httpModel.message];
        }
    }];
}
-(void)SQ_popNoTextView{
    NoTextView  * view = [[NoTextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH-55, 162)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
        [weakSelf.topView.textField becomeFirstResponder];
    }];
}

-(void)pushVIPAlert{
    VipView * view = [[VipView alloc]init];
    view.frame = CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT);
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view setOpenVIPBlock:^{
        [wk_pop dismiss];
        MeVipViewController * vc = [[MeVipViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
    [view setCloseViewBlock:^{
        [wk_pop dismiss];
    }];
}
-(void)SQ_popcodeView:(NSString*)message{
    ChartingImageNumberView  * view = [[ChartingImageNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 212)];
    view.messageLabel.text = message;
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    [view.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
}

- (ChartingTopView *)topView{
    if (!_topView) {
        _topView = [[ChartingTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, TopHeight+50)];
    }
    return  _topView;
}
- (ChartingFootView *)footView{
    if (!_footView) {
        _footView = [[ChartingFootView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 70)];
        [_footView.addBtn addTarget:self action:@selector(SQ_addImage) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _footView;
}

//提示是否给20积分
-(void)pushCreateAlert{
    ChartView * view = [[ChartView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    view.needIntegral = self.topView.needIntegral;
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view setClickOKBlock:^{
        [wk_pop dismiss];
        [weakSelf SQ_addImageHttps];
    }];
    [view setClickCancelBlock:^{
        [wk_pop dismiss];
    }];
}
//制图积分不足
-(void)pushChargeAlert{
    NoIntegral * view = [[NoIntegral alloc]init];
    view.frame = CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT);
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [view setClickOKBlock:^{
        ZSViewController * vc = [[ZSViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
        [wk_pop dismiss];
    }];
    [view setClickCancelBlock:^{
        [wk_pop dismiss];
    }];
}
@end
